package org.cobo.vmmanagement.web;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cobo.vmmanagement.domain.Charge;
import org.cobo.vmmanagement.domain.Project;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping("/projecthoursdetail/**")
@Controller
public class ProjectHoursDetail {

    @RequestMapping(value="/projecthoursdetail/project/{projectId}", method = RequestMethod.GET)
    public String get(@PathVariable Long projectId, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
		return this.post(projectId, modelMap, request, response);
    }
    
    @RequestMapping(method = RequestMethod.POST)
    public String post(@RequestParam("projectId") Long projectId, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    	Project project = Project.findProject(projectId);
    	List<Charge> charges = Charge.findChargesByProject(project).getResultList();
    	
    	// Calculate totals
    	int totalHours = 0;
    	int consultantHours = 0;
    	int seniorHours = 0;
    	int managerHours = 0;
    	int seniorManagerHours = 0;
    	int partnerHours = 0;
    	int qaPartnerHours = 0;
    	
    	Iterator<Charge> list = charges.iterator();
    	while(list.hasNext()) {
    		Charge tempCharge = list.next();
			int hours = tempCharge.getAmountOfHours().intValue();
			totalHours += hours;
			String category = tempCharge.getResource().getCategory().getName();
			// TODO: parametrize categories
			if(category.equals("Consultant")) {
				consultantHours += hours;
			} else if(category.equals("SeniorConsultant")) {
				seniorHours += hours;
			} else if(category.equals("Manager")) {
				managerHours += hours;
			} else if(category.equals("SeniorManager")) {
				seniorManagerHours += hours;
			} else if(category.equals("Partner")) {
				partnerHours += hours;
			} else if(category.equals("QAPartner")) {
				qaPartnerHours += hours;
			} else {
				// TODO: Error?
				//totalHours -= hours;
			}			
    	}
    	
    	// Add results to response
    	modelMap.addAttribute("totalHours", totalHours);
    	modelMap.addAttribute("consultantHours", consultantHours);
    	modelMap.addAttribute("seniorHours", seniorHours);
    	modelMap.addAttribute("managerHours", managerHours);
    	modelMap.addAttribute("seniorManagerHours", seniorManagerHours);
    	modelMap.addAttribute("partnerHours", partnerHours);
    	modelMap.addAttribute("qaPartnerHours", qaPartnerHours);
    	
    	modelMap.addAttribute("charges", charges);
    	
    	return "projecthoursdetail/charges";
    }

    @RequestMapping
    public String index() {
        return "projecthoursdetail/index";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String get(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    	List<Project> projects = Project.findAllProjects();
		// Get current user role
		String role = SecurityContextHolder.getContext().getAuthentication().getAuthorities().toString();
		boolean isManager = role.contains("ROLE_MANAGER") || role.contains("ROLE_ADMIN") ? true : false;
		
		modelMap.addAttribute("isManager", isManager);
    	modelMap.addAttribute("projects", projects);
    	
    	return "projecthoursdetail/index";
    }
}
